package cn.school.controller.app;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.SysUserUtils;
import cn.school.prevention.entity.FeedBack;
import cn.school.prevention.entity.Info;
import cn.school.prevention.entity.Scope;
import cn.school.prevention.service.FeedBackService;
import cn.school.prevention.service.InfoService;
import cn.school.prevention.service.ScopeService;
import cn.school.student.entity.StudentInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * App 学生 + 其他用户登录
 *
 * @author fangjin.tao
 * @updateTime 2022/12/27 13:41
 * @throws
 */
@RestController
@RequestMapping("/app/prevention")
@Api(tags = "PreventionController=> 学生防疫任务列表")
public class PreventionController {
	@Autowired
	private FeedBackService feedBackService;
	@Autowired
	private InfoService infoService;
	@Autowired
	private ScopeService scopeService;
	
	@ApiOperation("feedBackPageList => 分页查询反馈详情表")
	@PostMapping(value = "/feedBackPageList")
	public CommonResult<CommonPage<FeedBack>> feedBackPageList(@RequestBody FeedBack feedBack) {
		
		int pageSize = feedBack.getPageSize();
		int pageNum = feedBack.getPageNum();
		StudentInfo studentInfo = SysUserUtils.getStudentUser();
		if (studentInfo == null) {
			// 当前用户不是学生，则返回空列表
			IPage<FeedBack> page = new Page<FeedBack>(pageNum, pageSize);
			return CommonResult.success(CommonPage.restPage(page));
		}
		
		String todoFeedbackDate =feedBack.getTodoFeedbackDate();
		IPage<FeedBack> page = new Page<FeedBack>(pageNum, pageSize);
		LambdaQueryWrapper<FeedBack> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(FeedBack::getStudentId, studentInfo.getStudentId());
		if (StrUtil.isNotEmpty(todoFeedbackDate)) {
			queryWrapper.eq(FeedBack::getTodoFeedbackDate, todoFeedbackDate);
		} else {
			queryWrapper.lt(FeedBack::getTodoFeedbackDate, DateUtil.format(Calendar.getInstance().getTime(), "yyyy-MM-dd"));
		}
		queryWrapper.orderByDesc(FeedBack::getTodoFeedbackDate);
		page = feedBackService.page(page, queryWrapper);
		// 获取防疫标题信息
		if (page.getRecords() != null && page.getRecords().size() != 0) {
			Scope scope = null;
			Info info = null;
			for (FeedBack record : page.getRecords()) {
				scope = scopeService.getById(record.getScopeId());
				if (scope != null) {
					info = infoService.getById(scope.getPiId());
				}
				if (info != null) {
					record.setTitle(info.getTitle());
				}
			}
		}
		return CommonResult.success(CommonPage.restPage(page));
	}
	
	@ApiOperation("updateFeedBack => 修改反馈详情表")
	@PostMapping("/updateFeedBack")
	public CommonResult<Integer> updateFeedBack(@RequestBody FeedBack feedBack) {
		int num = feedBackService.updateByStudent(feedBack);
		if (num > 0) {
			return CommonResult.success();
		}
		return CommonResult.failed();
	}
	
	
}
